package com.sicnu.cheer.im.fragment;

import android.app.ProgressDialog;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.sicnu.cheer.generalmodule.bean.MyUser;
import com.sicnu.cheer.generalmodule.fragment.base.BaseFragment;
import com.sicnu.cheer.generalmodule.util.UIHelper;
import com.sicnu.cheer.im.R;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import io.rong.imkit.RongIM;
import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.UserInfo;

/**
 * Created by cheer on 2016/12/19.
 * 消息列表fragment
 */

public class MessageFragment extends BaseFragment implements RongIM.UserInfoProvider {
    private ProgressDialog progressDialog;
    private int i=0;
    private List<UserInfo> friends=new ArrayList<>();
    @Override
    protected int setLayoutResourceId() {
        return R.layout.fragment_message;
    }
    @Override
    protected void initView() {
        super.initView();
        RongIM.setUserInfoProvider(this, true);
        RongIM.getInstance().setMessageAttachedUserInfo(true);

        ConversationListFragment fragment = (ConversationListFragment) getChildFragmentManager().findFragmentById(R.id.conversation_list);

        Uri uri = Uri.parse("rong://" + getActivity().getApplicationInfo().packageName).buildUpon()
                .appendPath("conversationlist")
                .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //设置私聊会话非聚合显示
                .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "true")//设置群组会话聚合显示
                .appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "false")//设置讨论组会话非聚合显示
                .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "false")//设置系统会话非聚合显示
                .build();

        fragment.setUri(uri);
    }

    @Override
    protected void initData(Bundle arguments) {
        super.initData(arguments);
        progressDialog = new ProgressDialog(getContext());
        progressDialog.setMessage("加载中...");
        initUsers();
    }

    /**
     * 获取融云上所有该app的用户放进friends里面，待之后获取用户信息时取用
     */
    private void initUsers() {
        BmobQuery<MyUser> query = new BmobQuery<>();
        query.findObjects(getContext(), new FindListener<MyUser>() {
            @Override
            public void onSuccess(List<MyUser> list) {
                for (MyUser user :list) {
                    String userId = user.getObjectId();
                    String name = user.getNick();
                    String portraitUri = user.getHeadPic();
                    UserInfo userInfo =  new UserInfo(userId, name, Uri.parse(portraitUri));
                    friends.add(userInfo);
                }
            }
            @Override
            public void onError(int i, String s) {
                UIHelper.ToastMessage(mActivity,"查找用户信息失败:"+s);
            }
        });
    }

    /**
     * 根据用户ID获取用户信息
     * @param userId
     * @return
     */
    @Override
    public UserInfo getUserInfo(String userId) {
        for (UserInfo user : friends) {
            if (user.getUserId().equals(userId)){
                return user;
            }
        }
        return null;
    }

}
